﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Utils;
using WebMatrix.WebData;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class DashBoardController : Controller
    {
        public DashBoardModel ObjDashBoard;
        public OrganizationModel ObjOrganization;
        public ProjectModel ObjProject;
        public UsersModel ObjUsers;

        public DashBoardController()
        {
            ObjUsers = new UsersModel();
            ObjDashBoard = new DashBoardModel();
            ObjOrganization = new OrganizationModel();
            ObjProject = new ProjectModel();
        }

        //
        // GET: /Scrum/DashBoard/
        [HttpGet]
        public ActionResult Index()
        {
            Guid id = ObjUsers.GetUsersIdByEmail();
            Users user = ObjUsers.GetUsersById(id);
            Client.Instance.ClientId = id;
            Client.Instance.ClientName = user.FirstName + " " + user.LastName;
            IList<Organization> orgList = ObjOrganization.GetOrganizations();
            if (user.DefaultProject != null)
            {
                Client.Instance.DefaultProjectId = user.DefaultProject.Id;
                Client.Instance.CurrentProjectId = user.DefaultProject.Id;
            }
            if (orgList.Count == 0)
            {
                return RedirectToAction("Index", "Initialize");
            }
            else if (Client.Instance.DefaultProjectId == Guid.Empty)
            {
                foreach (Organization organization in orgList)
                {
                    Client.Instance.CurrentOrganizationId = organization.Id;
                }
                return RedirectToAction("Index", "Initialize");
            }
            else
            {
                var model = new TimeLineModel
                    {
                        TimeLineList = ObjDashBoard.GetTimeLines(),
                        TimeLine = new TimeLine()
                    };
                return View(model);
            }
        }

        // 
        // POST: /TimeLine/Create 
        [HttpPost]
        public ActionResult Index(TimeLine timeLine)
        {
            try
            {
                timeLine.Users = ObjUsers.GetUsersById(Client.Instance.ClientId);
                timeLine.Project = ObjProject.GetProjectById(Client.Instance.CurrentProjectId);
                timeLine.Time = DateTime.Now;
                ObjDashBoard.NewTimeLine(timeLine);
                return RedirectToAction("Index", "DashBoard");
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "DashBoard");
            }
        }

        //
        // GET:
        [HttpGet]
        public ActionResult Manage()
        {
            return RedirectToRoute("Account");
        }

        //
        // POST:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            if (HttpContext.Session != null) HttpContext.Session.Abandon();
            WebSecurity.Logout();
            Session.Remove("facebooktoken");

            return RedirectToRoute("Default", new {action = "Index", controller = "Home"});
        }
    }
}